package miiteen.customsportal.basic.service;

import miiteen.customsportal.basic.beans.HistoryView;
import miiteen.customsportal.basic.beans.HistoryViewBase;
import miiteen.customsportal.basic.dao.HistoryViewDao;
import miiteen.data.dao.BaseDao;
import miiteen.data.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service("historyViewService")
public class HistoryViewService extends BaseServiceImpl<HistoryView> {

    @Autowired
    private HistoryViewDao historyViewDao;

    @Override
    protected BaseDao<HistoryView> getDao() {
        return historyViewDao;
    }

    /**
     * 填充浏览量
     *
     * @param list
     * @param <T>
     */
    public <T extends HistoryViewBase> void fillVisitCount(List<T> list) {
        //获取Id集合
        String ids = list.stream().map(x -> x.getId()).collect(Collectors.joining("','"));
        ids = "'" + ids + "'";
        //获取访问量
        Map<String, Object> map = new HashMap<>();
        map.put("interiorIds", ids);
        List<HistoryView> visitList = findListByMap(map);
        Map<String, HistoryView> visitMap = visitList.stream().collect(Collectors.toMap(HistoryView::getInteriorId, Function.identity(), (k1, k2) -> k2));
        //填充
        list.forEach(x -> {
            HistoryView historyView = visitMap.get(x.getId());
            if (historyView != null) {
                x.setTotalNumber(historyView.getAllNumber());
                x.setTodayNumber(historyView.getDayNumber());
            }
        });
    }

}
